package accounts;

import com.google.appengine.api.datastore.DatastoreService;
import com.google.appengine.api.datastore.DatastoreServiceFactory;
import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;
import com.google.appengine.api.datastore.Query;
import com.google.appengine.api.datastore.Query.FilterOperator;

import java.io.IOException;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@SuppressWarnings("serial")
public class LockAccountServlet extends HttpServlet {
	
	private String searhKeyWord;
	private String username;

	public void doPost(HttpServletRequest req, HttpServletResponse resp) 
			throws IOException {

		this.username = req.getParameter("AccountKey");
		this.searhKeyWord = req.getParameter("keyword");
		
		DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
		Key accountKey = KeyFactory.createKey("Accounts", "accountName");
		
		Query query = new Query("Account", accountKey).addFilter("username", FilterOperator.EQUAL, this.username);
		Entity user = datastore.prepare(query).asSingleEntity();
		
		if(!user.equals(null)){
			user.setProperty("lockStatus", "Locked");
			datastore.put(user);
			if(this.searhKeyWord.equals("all")){
				resp.sendRedirect("/admin_viewAllUsers.jsp");
			} else {
				resp.sendRedirect("/admin_userSearchResults.jsp?&keyword="+searhKeyWord);
			}
		}
		else
			resp.getWriter().println("Error! - Not such user in the database");
		/*
		if(this.searhKeyWord.equals("all")){
			resp.sendRedirect("/admin_viewAllUsers.jsp");
		} else {
			resp.sendRedirect("/admin_userSearchResults.jsp?&keyword="+searhKeyWord);
		}*/
	}
}